package com.qpkj.web.controller.oms;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.qpkj.common.core.domain.entity.SysUser;
import com.qpkj.oms.parameter.ProductSaveVo;
import com.qpkj.oms.parameter.StockVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.qpkj.common.annotation.Log;
import com.qpkj.common.core.controller.BaseController;
import com.qpkj.common.core.domain.AjaxResult;
import com.qpkj.common.enums.BusinessType;
import com.qpkj.oms.domain.OmsProduct;
import com.qpkj.oms.service.IOmsProductService;
import com.qpkj.common.utils.poi.ExcelUtil;
import com.qpkj.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物料Controller
 *
 * @author ruoyi
 * @date 2022-12-20
 */
@RestController
@RequestMapping("/oms/product")
public class OmsProductController extends BaseController {
    @Autowired
    private IOmsProductService omsProductService;

    /**
     * 查询物料列表
     */
    @PreAuthorize("@ss.hasPermi('oms:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(OmsProduct omsProduct) {
        startPage();
        List<OmsProduct> list = omsProductService.selectOmsProductList(omsProduct);
        return getDataTable(list);
    }

    /**
     * 导出物料列表
     */
    @PreAuthorize("@ss.hasPermi('oms:product:export')")
    @Log(title = "物料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OmsProduct omsProduct) {
        List<OmsProduct> list = omsProductService.selectOmsProductList(omsProduct);
        ExcelUtil<OmsProduct> util = new ExcelUtil<OmsProduct>(OmsProduct.class);
        util.exportExcel(response, list, "物料数据");
    }

    /**
     * 获取物料详细信息
     */
    @PreAuthorize("@ss.hasPermi('oms:product:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId) {
        return success(omsProductService.selectOmsProductByProductId(productId));
    }

    @PreAuthorize("@ss.hasPermi('oms:product:query')")
    @GetMapping(value = "/queryProduct")
    public AjaxResult queryProduct(@RequestParam("productNo") String productNo, @RequestParam("type") Integer type) {
        return omsProductService.selectOmsProductByProductNo(productNo,type);
    }

    /**
     * 新建任务，根据物料编号，查询生产所需
     * @param productNo
     * @param type
     * @return
     */
    @PreAuthorize("@ss.hasPermi('oms:product:query')")
    @GetMapping(value = "/queryProductByNo")
    public AjaxResult queryProductByNo(@RequestParam("productNo") String productNo
            , @RequestParam("type") Integer type) {
        return omsProductService.queryProductByNo(productNo, type);
    }

    @GetMapping(value = "/queryProductByName")
    public AjaxResult queryProductByName(@RequestParam("productName") String productName
            , @RequestParam("type") Integer type) {
        return omsProductService.queryProductByName(productName, type);
    }

    /**
     * 新增物料
     */
    @PreAuthorize("@ss.hasPermi('oms:product:add')")
    @Log(title = "物料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ProductSaveVo productSaveVo) {
        return omsProductService.insertOmsProduct(productSaveVo);
    }

    /**
     * 重写修改物料
     */
    @Log(title = "物料", businessType = BusinessType.UPDATE)
    @PostMapping("/materialUpdate")
    public AjaxResult materialUpdate(@Validated @RequestBody ProductSaveVo productSaveVo) {
        return toAjax(omsProductService.materialUpdate(productSaveVo));
    }
    /**
     * 修改物料
     */
    @PreAuthorize("@ss.hasPermi('oms:product:edit')")
    @Log(title = "物料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OmsProduct omsProduct) {
        return toAjax(omsProductService.updateOmsProduct(omsProduct));
    }

    /**
     * 删除物料
     */
    @PreAuthorize("@ss.hasPermi('oms:product:remove')")
    @Log(title = "物料", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds) {
        return toAjax(omsProductService.deleteOmsProductByProductIds(productIds));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<OmsProduct> util = new ExcelUtil<OmsProduct>(OmsProduct.class);
        util.importTemplateExcel(response, "物料数据");
    }

    @Log(title = "物料管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('oms:product:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<OmsProduct> util = new ExcelUtil<OmsProduct>(OmsProduct.class);
        List<OmsProduct> productList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = omsProductService.importProduct(productList, updateSupport, operName);
        return success(message);
    }

    @Log(title = "物料管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('oms:product:edit')")
    @PostMapping("/productStock")
    public AjaxResult productStock(@RequestBody StockVo stockVo) {
        return omsProductService.productStock(stockVo);
    }
}
